
package edu.hm.devcamp.presentation.overlays;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

import edu.hm.devcamp.R;
import edu.hm.devcamp.presentation.LocationListenerImpl;

public class PoiOverlay extends ItemizedOverlay {

    private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    Context mContext;

    public PoiOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
    }

    public PoiOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
    }
    
    public void populateOverlays(){
        populate();
    }
    
    public void addOverlay(List<OverlayItem> all) {
        mOverlays.addAll(all);
    }
    
    public List<OverlayItem> getAll(){
        return mOverlays;
    }

    AlertDialog mDialog;

    @Override
    protected boolean onTap(int index) {

        final OverlayItem item = mOverlays.get(index);

        final LayoutInflater inflater = LayoutInflater.from(mContext);
        final View view = inflater.inflate(R.layout.dialog, null);

        final OnClickListener listener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.navigation:
                        final GeoPoint point = item.getPoint();
                        final Intent intent = new Intent(Intent.ACTION_VIEW,
                                Uri.parse("google.navigation:q=" + (point.getLatitudeE6() / 1E6)
                                        + "," + (point.getLongitudeE6() / 1E6)));
                        try {
                            mContext.startActivity(intent);
                        } catch (ActivityNotFoundException e) {
                            try {
                                final GeoPoint curPoint = LocationListenerImpl.currentLocation;
                                if (curPoint != null) {
                                    final Intent mapsIntent = new Intent(Intent.ACTION_VIEW,
                                            Uri.parse("http://maps.google.com/maps?saddr="
                                                    + curPoint.getLatitudeE6() / 1e6 + ","
                                                    + curPoint.getLongitudeE6() / 1e6 + "&daddr="
                                                    + point.getLatitudeE6() / 1e6 + ","
                                                    + point.getLongitudeE6() / 1e6));
                                    mContext.startActivity(mapsIntent);
                                } else {
                                    Toast.makeText(mContext, R.string.no_navigation,
                                            Toast.LENGTH_LONG).show();
                                }
                            } catch (ActivityNotFoundException ex) {
                                Toast.makeText(mContext, R.string.no_navigation, Toast.LENGTH_LONG)
                                        .show();
                            }
                        }
                        break;
                    case R.id.cancel:
                        if (mDialog != null && mDialog.isShowing()) {
                            mDialog.dismiss();
                        }
                }
            }
        };

        final TextView header = (TextView)view.findViewById(R.id.header);
        final TextView body = (TextView)view.findViewById(R.id.body);
        final ImageButton navigation = (ImageButton)view.findViewById(R.id.navigation);
        final ImageButton cancel = (ImageButton)view.findViewById(R.id.cancel);

        header.setText(item.getTitle());
        body.setText(item.getSnippet());
        navigation.setOnClickListener(listener);
        cancel.setOnClickListener(listener);
        
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(view);
        mDialog = builder.create();
        mDialog.show();

        return true;
    }    
}
